Completed
Push — master ( 6eff60...46c46b )
by
unknown
01:53
created

abe-plugins.js ➔ ???   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 74
rs 9.0335

1 Function

Rating   Name   Duplication   Size   Complexity  
C abe-plugins.js ➔ ... ➔ ??? 0 65 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import fse from 'fs-extra'
2
import clc from 'cli-color'
3
import extend from 'extend'
4
import path from 'path'
5
import {
6
  Util,
0 ignored issues
show
Unused Code introduced by
The variable Util seems to be never used. Consider removing it.
Loading history...
7
  FileParser,
8
  config,
9
  fileUtils,
0 ignored issues
show
Unused Code introduced by
The variable fileUtils seems to be never used. Consider removing it.
Loading history...
10
  folderUtils,
11
  cli,
0 ignored issues
show
Unused Code introduced by
The variable cli seems to be never used. Consider removing it.
Loading history...
12
  log
0 ignored issues
show
Unused Code introduced by
The variable log seems to be never used. Consider removing it.
Loading history...
13
} from '../'
14
15
let singleton = Symbol()
16
let singletonEnforcer = Symbol()
17
18
class Plugins {
19
20
  constructor(enforcer) {
21
    if(enforcer != singletonEnforcer) throw 'Cannot construct Plugins singleton'
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
22
    this._plugins = []
23
    this.fn = []
24
    var pluginsDir = path.join(config.root, config.plugins.url)
25
    if(folderUtils.isFolder(pluginsDir)) {
26
      this._plugins = FileParser.getFolders(pluginsDir, true, 0)
27
      Array.prototype.forEach.call(this._plugins, (plugin) => {
28
        // has hooks
29
        var plugHooks = path.join(plugin.path, config.hooks.url)
30
        if(folderUtils.isFolder(plugHooks)) {
31
          var plugHooksFile = path.join(plugHooks, 'hooks.js')
32
          var h = require(plugHooksFile)
33
          plugin.hooks = h.default
34
        }else {
35
          plugin.hooks = null
36
        }
37
38
        // has partials
39
        var plugPartials = path.join(plugin.path, config.pluginsPartials)
40
        if(folderUtils.isFolder(plugPartials)) {
41
          plugin.partials = plugPartials
42
        }else {
43
          plugin.partials = null
44
        }
45
46
        // has templates
47
        var plugTemplates = path.join(plugin.path, config.templates.url)
48
        if(folderUtils.isFolder(plugTemplates)) {
49
          plugin.templates = plugTemplates
50
        }else {
51
          plugin.templates = null
52
        }
53
54
        // has process
55
        var plugProcess = path.join(plugin.path, 'process')
56
        if(folderUtils.isFolder(plugProcess)) {
57
          plugin.process = {}
58
          var processFiles = FileParser.getFiles(plugProcess, true, 0)
59
          Array.prototype.forEach.call(processFiles, (processFile) => {
60
            plugin.process[processFile.cleanNameNoExt] = processFile.path
61
          })
62
        }else {
63
          plugin.process = null
64
        }
65
66
        // has routes
67
        var plugRoutes = path.join(plugin.path, 'routes')
68
        if(folderUtils.isFolder(plugRoutes)) {
69
          plugin.routes = {}
70
71
          var gets = path.join(plugRoutes, 'get')
72
          if(folderUtils.isFolder(gets)) {
73
            var routesGet = FileParser.getFiles(gets, true, 0)
74
            Array.prototype.forEach.call(routesGet, (route) => {
75
              route.routePath = `/abe/plugin/${plugin.name}/${route.name.replace('.js', '')}*`
76
            })
77
            plugin.routes.get = routesGet
78
          }
79
80
          var posts = path.join(plugRoutes, 'post')
81
          if(folderUtils.isFolder(posts)) {
82
            var routesPost = FileParser.getFiles(posts, true, 0)
83
            Array.prototype.forEach.call(routesPost, (route) => {
84
              route.routePath = `/abe/plugin/${plugin.name}/${route.name.replace('.js', '')}*`
85
            })
86
            plugin.routes.post = routesPost
87
          }
88
        }else {
89
          plugin.routes = null
90
        }
91
      })
92
    }
93
  }
94
95
  static get instance() {
96
    if(!this[singleton]) {
97
      this[singleton] = new Plugins(singletonEnforcer)
98
    }
99
    return this[singleton]
100
  }
101
102
  getProcess(fn) {
103
    var proc = null
104
    if(typeof this._plugins !== 'undefined' && this._plugins !== null) {
105
      Array.prototype.forEach.call(this._plugins, (plugin) => {
106
        if(typeof plugin.process !== 'undefined' && plugin.process !== null
107
          && typeof plugin.process[fn] !== 'undefined' && plugin.process[fn] !== null) {
108
          proc = plugin.process[fn]
109
        }
110
      })
111
    }
112
113
    return proc
114
  }
115
116
  hooks() {
117
    if(arguments.length > 0) {
118
      var args = [].slice.call(arguments)
119
      var fn = args.shift()
120
121
      if(typeof this._plugins !== 'undefined' && this._plugins !== null) {
122
        Array.prototype.forEach.call(this._plugins, (plugin) => {
123
          if(typeof plugin.hooks !== 'undefined' && plugin.hooks !== null
124
            && typeof plugin.hooks[fn] !== 'undefined' && plugin.hooks[fn] !== null) {
125
            args[0] = plugin.hooks[fn].apply(this, args)
126
          }
127
        })
128
      }
129
    }
130
131
    return args[0]
0 ignored issues
show
Bug introduced by
The variable args does not seem to be initialized in case arguments.length > 0 on line 117 is false. Are you sure this can never be the case?
Loading history...
132
  }
133
134
  getHooks() {
135
    return this._plugins
136
  }
137
138
  getPartials() {
139
    var partials = []
140
    Array.prototype.forEach.call(this._plugins, (plugin) => {
141
      if(typeof plugin.partials !== 'undefined' && plugin.partials !== null) {
142
        partials.push(plugin.partials)
143
      }
144
    })
145
146
    return partials
147
  }
148
149
  getRoutes() {
150
    var routes = []
151
    Array.prototype.forEach.call(this._plugins, (plugin) => {
152
      if(typeof plugin.routes !== 'undefined' && plugin.routes !== null) {
153
        routes = routes.concat(plugin.routes)
154
      }
155
    })
156
157
    return routes
158
  }
159
}
160
161
export default Plugins